package com.zhongying.jf.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * SerializeObjUtil.java
 * 
 * @Description: TODO(描述这个类的作用)
 * 
 * @version
 * @Creator haiwang @Date:2015-12-24 下午5:24:02
 * @Copyright gc-isoft 2002-2015
 */
public class SerializeObjUtil {
	/**
	 * 
	 * @Title: objToByte
	 * @Description: 序列化
	 * @param obj
	 * @return
	 */
	public static byte[] objectToByte(Object obj) {
		byte[] bytes = null;
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
			bo.close();
			oo.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bytes;
	}

	/**
	 * 
	 * @Title: ByteToObject
	 * @Description: 反序列化
	 * @param bytes
	 * @return
	 */
	public static Object byteToObject(byte[] bytes) {
		Object object = null;
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			object = ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
}
